//
//  RippleView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2018/3/12.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "RippleView.h"
#import "Const_OC.h"

#define width (130 * kScreenWidth / 375)
@interface RippleView()

@property (nonatomic, strong)UIView * centerView;

@property (nonatomic, strong)NSTimer * timer;
@end
@implementation RippleView

- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self setup];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)setup {

    self.centerView = [[UIView alloc] initWithFrame:CGRectMake(width/4,width/4, width/2, width/2)];
    self.centerView.layer.cornerRadius = width/4;
    self.centerView.clipsToBounds = YES;
    self.centerView.backgroundColor = KThemeColor;
    [self insertSubview:self.centerView atIndex:0];
    
    self.
    self.timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(addRippleLayer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)startAnimation {
    
    [self.timer setFireDate:[NSDate distantPast]];
}

- (void)stopAnimation {
    
    [self.timer setFireDate:[NSDate distantFuture]];
}
- (void)addRippleLayer
{
    CAShapeLayer *rippleLayer = [[CAShapeLayer alloc] init];
    rippleLayer.position = CGPointMake(width/2, width/2);
    rippleLayer.bounds = CGRectMake(0, 0, width, width);
    rippleLayer.backgroundColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(width/4, width/4, width/2, width/2)];
    rippleLayer.path = path.CGPath;
    rippleLayer.strokeColor = KThemeColor.CGColor;
    rippleLayer.fillColor = KThemeColor.CGColor;
    
    
    [self.layer insertSublayer:rippleLayer below:self.centerView.layer];
    
    //addRippleAnimation
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(width/4, width/4, width/2, width/2)];
    CGRect endRect = CGRectInset(CGRectMake(0, 0, width, width),0,0);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    
    rippleLayer.path = endPath.CGPath;
    rippleLayer.opacity = 0.0;
    
    CABasicAnimation *rippleAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    rippleAnimation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
    rippleAnimation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    rippleAnimation.duration = 1.5;
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = [NSNumber numberWithFloat:0.6];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.0];
    opacityAnimation.duration = 1.5;
    
    [rippleLayer addAnimation:opacityAnimation forKey:@""];
    [rippleLayer addAnimation:rippleAnimation forKey:@""];
    
    [self performSelector:@selector(removeRippleLayer:) withObject:rippleLayer afterDelay:1.5];
}

- (void)removeRippleLayer:(CAShapeLayer *)rippleLayer
{
    [rippleLayer removeFromSuperlayer];
    rippleLayer = nil;
}

- (void)closeRippleTimer
{
    if (_timer) {
        if ([_timer isValid]) {
            [_timer invalidate];
        }
        _timer = nil;
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
